<?php defined('BASEPATH') or exit('No direct script access allowed');

class Ranking_m extends MY_Model {

	function _regions( $selected_id = 0 ){
		$regions_obj_arr = $this->db->get('ff_regions')->result();

		$html = "<select name='region' id='region' onchange='javascript:selectCities(this.value);' class='select_postalcode' >";
		
		$select = ( $selected_id == 0 )? ' selected="selected"' : '';
		$html .= "<option value=\"0\" $select>".lang('ranking.select.regions').'</option>';
		
		foreach($regions_obj_arr as $obj){
			if($obj->id == $selected_id){
				$select = ' selected="selected"';
			}else{
				$select = '';
			}
			
			$html .= "<option value='$obj->id' $select>".html_entity_decode( ucfirst( $obj->name ) )."</option>";
		}
		$html .= "</select>";
		
		return $html;
	}
	
	function _cities( $region_id = -1 , $selected_id = 0 ){
		if( $region_id == -1 ){
			$cities_obj_arr = $this->db->get('ff_cities')->result();
		}else{
			$cities_obj_arr = $this->db->where('region_id',$region_id)->get('ff_cities')->result();
		}
		
		$html = "<select name='city' id='city' class='select_postalcode' >";
		$html .= '<option value="0">'.lang('ranking.select.cities').'</option>';
		
		// IF NOT CHOOSE REGIONS. CITIES IS DISABLE
		if($region_id != -1){
			foreach ($cities_obj_arr as $obj){
				if($obj->id == $selected_id){
					$select = ' selected="selected"';
				}else{
					$select = '';
				}
				
				$html .= "<option value='$obj->id' $select>".html_entity_decode( ucfirst( $obj->city ) )."</option>";
			}
		}
		$html .= "</select>";
		
		return $html;
	}

	function get_2_latest_items(){
		$r= $this->db->query("SELECT * FROM ff_rs_content ORDER BY ff_rs_content.id DESC LIMIT 2")->result();
		return $r;
	}
	
	function get_rank_items($page){
		$this->load->library('module_helper');
		$active_ranking = $this->module_helper->getSetting('active_ranking');
		
		$r= $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking as rank FROM ff_rs_content WHERE
							 1 ORDER BY rank ASC,modified_time DESC ")->result_array();//LIMIT ".($page-1)*35 .",35
		return $r;
	}
	
	function get_2_new_post_connection(){
		$this->load->library('module_helper');
		$active_ranking = $this->module_helper->getSetting('active_ranking');
		
		$res = $this->db->query("SELECT * FROM rs_connection ORDER BY last_posted_time DESC LIMIT 1")->result();
		if(empty($res)) return;
		
		$r1= $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking as rank FROM ff_rs_content WHERE
								id =".$res[0]->upper_content_id)->result();
		$r2= $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking as rank FROM ff_rs_content WHERE
								id =".$res[0]->lower_content_id)->result();
		$obj_array = array();
		$obj_array[] = $r1[0];
		$obj_array[] = $r2[0];
		 
		return $obj_array;
	}
	
	function get_region_name($region_id= 0){
		$r = $this->db->where('id',$region_id)->get('ff_regions')->result();
		if(empty($r)) return '';
		return $r[0]->name;
	}
	
	function get_city_name($city_id=0){
		$r= $this->db->where('id',$city_id)->get('ff_cities')->result();
		if(empty($r)) return '';
		return $r[0]->city;
	}
	
	function search_item($region_id, $city_id, $key, $by){
		$this->load->library('module_helper');
		$active_ranking = $this->module_helper->getSetting('active_ranking');
		
		if( $region_id== 0 AND $city_id== 0 AND $key== '' AND $by== 'all' ){
			return $this->get_rank_items($page=1);
		}elseif( $region_id== 0 AND $city_id== 0 AND $by== 'region' ){
			return $this->get_rank_items($page=1);
		}elseif( $key== '' AND $by== 'key' ){
			return $this->get_rank_items($page=1);
		}	
	 
		if($by == 'region'){
			if($city_id != 0){
				$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE
								region_id=$region_id AND city_id=$city_id 
								ORDER BY rank ASC ")->result_array();
			}else{
				$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE
								region_id=$region_id 
								ORDER BY rank ASC ")->result_array();
			}
		}elseif($by == 'key'){
			$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE 
							    name LIKE '%$key%' 
								ORDER BY rank ASC ")->result_array();
		}else{
			if($region_id== 0 AND $city_id== 0){
				$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE
							    name LIKE '%$key%' 
								ORDER BY rank ASC ")->result_array();		
			}elseif( $key== '' ){
				if($city_id != 0){
					$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE
								region_id=$region_id AND city_id=$city_id 
								ORDER BY rank ASC ")->result_array();
				}else{
					$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE
									region_id=$region_id 
									ORDER BY rank ASC ")->result_array();
				}
			}else{
				$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE 
								region_id=$region_id AND city_id=$city_id AND name LIKE '$key'
								ORDER BY rank ASC ")->result_array();
			}
		}
		if(!empty($r)){
			$arr_id = array();
			foreach($r as &$a){
				$a['search_item'] = ' search';
				$arr_id[] = $a['id'];
			}
			$list_id = implode(',', $arr_id);
			
			$rr= $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking as rank FROM ff_rs_content WHERE
							 1 ORDER BY rank  ASC,modified_time DESC ")->result_array();
			foreach($rr as &$ra){
				if(in_array($ra['id'],$arr_id)){
					$ra['search_item'] = ' search';
				}
			}				
			return $rr;	
			//return array_merge($r, $rr);
		}else{
			$rr= $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking as rank FROM ff_rs_content WHERE
							 1 ORDER BY rank  ASC,modified_time DESC ")->result_array();
			return $rr;
		}
		
	}
	
	function get_my_ranking_items($user_id){
		$this->load->library('module_helper');
		$active_ranking = $this->module_helper->getSetting('active_ranking');
		
		$res = $this->db->query("SELECT * FROM rs_connection INNER JOIN rs_post 
								ON rs_connection.id=rs_post.connection_id WHERE rs_post.user_id=$user_id ")->result();
		if(empty($res)) return null;
		
		$content_id_arr = array();
		foreach($res as $one){
			$content_id_arr[] = $one->upper_content_id;
			$content_id_arr[] = $one->lower_content_id;
		}
		$content_id_arr = array_unique($content_id_arr);
		
		$r = $this->db->query("SELECT ff_rs_content.*,ff_rs_content.$active_ranking AS rank FROM ff_rs_content WHERE id IN(".implode(',', $content_id_arr).") ORDER BY rank ASC")->result_array();
		return $r;
	}
	
	function posts($data = array())
	{
		$limit		= $data['limit'];
		$category	= $data['category'];
		$order_by 	= $data['order_by'];
		$order_dir	= $data['order-dir'];

		if ($category)
		{
			$this->db->where('c.' . (is_numeric($category) ? 'id' : 'slug'), $category);
		}

		return $this->db
			->select("blog.*, c.title as category_title, c.slug as category_slug")
			->where('status', 'live')
			->where('created_on <=', now())
			->join('blog_categories c', 'blog.category_id = c.id', 'LEFT')
			->order_by('blog.' . $order_by, $order_by)
			->limit($limit)
			->get('blog')
			->result_array();
	}
	
	
	 
	
	
	
}	